package com.future.direction.common.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.MaterialDialog;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.future.direction.App;
import com.future.direction.R;
import com.future.direction.data.bean.VersionBean;
import com.future.direction.ui.activity.BaseActivity;
import com.future.direction.ui.widget.MaterialViewDialog;

import java.io.File;
import java.text.DecimalFormat;

import io.reactivex.functions.Consumer;

/**
 * Created by ZP on 2018/8/17.
 */
public class UpdateVerUtil {

    public static final String APK_DOWNLOAD_PATH = "/Android/data/" + App.getApplication().getPackageName() + "/apk/";
    private BaseActivity activity;
    private TextView tvProgress;
    private ProgressBar pbBar;
    private File updateFile;
    private MaterialViewDialog dialog;
    private boolean isForced = true;//是否需要强制升级

    public UpdateVerUtil(BaseActivity activity) {
        this.activity = activity;
    }

    /**
     * 版本更新的提示
     *
     * @param versionBean 没有最新版本信息则返回null
     */
    public void updateVer(VersionBean versionBean) {
        //弹出提示框
        if (null != versionBean && !versionBean.getVersionName().equals(UIUtil.getVersionName())) {
            isForced = versionBean.isForce();
            if (activity.isFinishing()) return;
            final MaterialDialog dialog = new AlertDialogUtil().createDialog(activity);
            dialog.content(versionBean.getPrompt());
            if (isForced) {
                dialog.btnTextColor(UIUtil.getColor(R.color.colorBlack3B4059));
                dialog.btnNum(1);
                dialog.btnText("立即升级");
                dialog.setOnBtnClickL(new OnBtnClickClick(dialog, versionBean.getApkUrl()));
            } else {
                dialog.btnText("以后再说", "立即升级");
                dialog.setOnBtnClickL(null, new OnBtnClickClick(dialog, versionBean.getApkUrl()));
            }
            dialog.widthScale(0.85f);
            dialog.title("更新提示")
                    .show();
            dialog.setCancelable(!isForced);
            dialog.setCanceledOnTouchOutside(!isForced);
        }

    }

    /**
     * 查询版本更新
     *
     * @param versionBean 没有最新版本信息则返回null
     */
    public void updateVerClick(VersionBean versionBean) {
        //弹出提示框
        if (null != versionBean && !versionBean.getVersionName().equals(UIUtil.getVersionName())) {
            updateVer(versionBean);
        } else {  //没有更新
            if (activity.isFinishing()) return;
            final MaterialDialog dialog = new AlertDialogUtil().createDialog(activity);
            dialog.content("您当前的版本已经是最新版本！\n版本号：V" + UIUtil.getVersionName())
                    .btnText("确定")
                    .btnNum(1)
                    .btnTextColor(activity.getResources().getColor(R.color.colorAccent))
                    .title("更新提示")
                    .widthScale(0.85f)
                    .show();
        }
    }

    private class OnBtnClickClick implements OnBtnClickL {
        String updateUrl;
        MaterialDialog dialog;

        OnBtnClickClick(MaterialDialog dialog, String updateUrl) {
            this.dialog = dialog;
            this.updateUrl = updateUrl;
        }

        @Override
        public void onBtnClick() {
            if (StringUtil.isNotNullString(updateUrl)) {
                activity.requestSdWritePermissionStatic(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        // 在android 6.0之前会默认返回true
                        if (aBoolean) {
                            // 已经获取权限,请求检查更新
                            dialog.cancel();
                            showDownloadDialog(activity, updateUrl);
                        } else {
                            // 未获取权限
                            UIUtil.showToastSafe("获取权限失败,更新失败");
                        }
                    }
                });

            }
        }
    }


    /**
     * 升级下载的dialog
     *
     * @param activity
     */
    public void showDownloadDialog(final Activity activity, String apkDownPath) {
        dialog = new MaterialViewDialog(activity);
        View contentView = activity.getLayoutInflater().inflate(R.layout.layout_download_dialog, null);
        tvProgress = contentView.findViewById(R.id.tvProgress);
        pbBar = contentView.findViewById(R.id.pbBar);
        dialog.setCanceledOnTouchOutside(false);
        dialog.contentView(contentView);
        dialog.hasTitle(false)
                .bgColor(0)
                .widthScale(0.85f)
                .show();
        // 创建文件
        updateFile = new File(getSDCardPath() + APK_DOWNLOAD_PATH);
        if (!updateFile.exists()) {
            try {
                updateFile.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        File[] childFile = updateFile.listFiles();
        if (childFile.length != 0) {
            for (File f : childFile) {
                f.delete();
            }
        }
        updateFile = new File(updateFile, apkDownPath.substring(apkDownPath.lastIndexOf("/") + 1));
        if (updateFile.exists()) {
            try {
                updateFile.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 开始下载
        FileDownloader.getImpl()
                .create(apkDownPath)
                .setPath(updateFile.getPath()).setListener(new FileDownloadListener() {
            @Override
            protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {

            }

            @Override
            protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                double x_double = soFarBytes * 1.0;
                double tempresult = x_double / totalBytes;
                DecimalFormat df1 = new DecimalFormat("0.00"); // ##.00%
                // 百分比格式，后面不足2位的用0补齐
                String result = df1.format(tempresult);
                tvProgress.setText("下载进度：" + (int) (Float.parseFloat(result) * 100) + "%");
                pbBar.setMax(100);
                pbBar.setProgress((int) (Float.parseFloat(result) * 100));
            }

            @Override
            protected void completed(BaseDownloadTask task) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                //强制升级必须关闭activity
                if (isForced) {
                    activity.finish();
                }
                // 下载成功
                installApk(updateFile, activity);
            }

            @Override
            protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

            }

            @Override
            protected void error(BaseDownloadTask task, Throwable e) {
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                UIUtil.showToastSafe("下载失败,请重试！");
            }

            @Override
            protected void warn(BaseDownloadTask task) {

            }
        }).start();
    }


    // 获取SDCard的目录路径功能
    public static String getSDCardPath() {
        File sdcardDir = null;
        // 判断SDCard是否存在
        boolean sdcardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (sdcardExist) {
            sdcardDir = Environment.getExternalStorageDirectory();
        }
        return sdcardDir.toString();
    }


    // 下载完成后打开安装apk界面
    public static void installApk(File file, Context context) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

}
